package com.app.newcsc.fragment.moneydetail;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.app.newcsc.BR;
import com.app.newcsc.R;
import com.app.newcsc.app.AppViewModelFactory;
import com.app.newcsc.databinding.FragmentDetailBinding;
import com.app.newcsc.entity.DetailEntity;
import com.app.newcsc.utils.SpUtils;

import java.util.ArrayList;

import me.goldze.mvvmhabit.base.BaseFragment;

public class DetailFragment extends BaseFragment<FragmentDetailBinding, FundMoneyDetailViewModel> {

    public String coin_id;

    private DetailAdapter adapter;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_detail;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {
        super.initParam();
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    @Override
    public FundMoneyDetailViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(FundMoneyDetailViewModel.class);
    }

    @Override
    public void initData() {
        super.initData();
        binding.ivTitleBack.setOnClickListener(v -> getActivity().finish());
        SpUtils.put(getActivity(), "isLogin", "1");
        if (getArguments() != null) {
            coin_id = getArguments().getString("coin_id");
        }

        adapter = new DetailAdapter(R.layout.item_detail);
        binding.mRecyclerView.setAdapter(adapter);
        binding.mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        viewModel.lockLog(coin_id);
        viewModel.mDetailEntity.observe(this, new Observer<ArrayList<DetailEntity>>() {

            @Override
            public void onChanged(ArrayList<DetailEntity> detailEntities) {
                adapter.setNewInstance(detailEntities);
            }
        });



    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        SpUtils.put(getActivity(), "isLogin", "");
    }
}
